package com.niannian;

import java.io.File;

import cn.jpush.android.api.JPushInterface;
import com.niannian.db.DatabaseApi;
import com.niannian.imgCache.BitmapUtils;
import com.niannian.util.Common;
import com.niannian.util.UserInfoManager;
import com.umeng.analytics.MobclickAgent;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;

public class MyApplication extends Application {

	private DatabaseApi databaseapi;
	private boolean updateFriend = false;
	private boolean checkUpdate = false;
	public static BitmapUtils finalbitmap;
	private static MyApplication myApplication;
	public static Context mcontext;
	public static String FILE_PATH_TEMP;
	public static String FILE_PATH_IMAGES_SMALL;
	public static String FILE_PATH_IMAGES_MIDDLE;
	public static String FILE_PATH_IMAGES_BIG;
	public static String FILE_PATH_AUDIO;

	public final static int databaseversion = 7;
	public static String dbdir = "";
	public static String dbfile = "niannian.db";

	public static final int UPDATE_VERSION_NOTIFICATION_ID = 0;
	public static final int UPLOAD_NOTIFICATION_ID = 1;

	@Override
	public void onCreate() {
		super.onCreate();
		myApplication = this;
		init_app_path();
		databaseapi = DatabaseApi.getDataBaseApi(this);
		databaseapi.InstallDatabase();
		UserInfoManager.getUserInfoInstance(getApplicationContext());
		JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
		JPushInterface.init(this);
		//JPushInterface.stopPush(this);// 初始化 JPush

		mcontext = this.getApplicationContext();
		finalbitmap = new BitmapUtils(mcontext, FILE_PATH_IMAGES_BIG);
		// finalbitmap.configDefaultLoadingImage(APP.IMG_LOAD_HOLDER);
		// finalbitmap.configDefaultLoadFailedImage(APP.IMG_LOAD_FAILD);
		finalbitmap.configDefaultBitmapConfig(Bitmap.Config.RGB_565);
		finalbitmap.configDefaultAutoRotation(false);
		finalbitmap.configMemoryCacheEnabled(true);
		finalbitmap.configDiskCacheEnabled(true);

		// 友盟统计
		MobclickAgent.setDebugMode(false);
		MobclickAgent.openActivityDurationTrack(false);
		MobclickAgent.updateOnlineConfig(this);
	};

	public void init_app_path() {

		String filePath = "";
		filePath = getAppPath();
		if (Common.isSDcardExist()) {
			filePath = Common.getSDFilePath();
		} else {
			filePath = getAppPath();
		}

		FILE_PATH_TEMP = filePath + "temp/";
		FILE_PATH_IMAGES_SMALL = filePath + "images/small/";
		FILE_PATH_IMAGES_MIDDLE = filePath + "images/middle/";
		FILE_PATH_IMAGES_BIG = filePath + "images/big/";
		FILE_PATH_AUDIO = filePath + "audio/";

		File isPath = new File(FILE_PATH_TEMP);
		if (!isPath.exists()) {
			isPath.mkdirs();
		}
		isPath = new File(FILE_PATH_IMAGES_SMALL);
		if (!isPath.exists()) {
			isPath.mkdirs();
		}
		isPath = new File(FILE_PATH_IMAGES_MIDDLE);
		if (!isPath.exists()) {
			isPath.mkdirs();
		}
		isPath = new File(FILE_PATH_IMAGES_BIG);
		if (!isPath.exists()) {
			isPath.mkdirs();
		}
		isPath = new File(FILE_PATH_AUDIO);
		if (!isPath.exists()) {
			isPath.mkdirs();
		}

		dbdir = getAppPath() + "databases";

	}

	public String getAppPath() {
		return getApplicationContext().getFilesDir().getParent() + "/.";
	}

	public synchronized static MyApplication getInstance() {
		if (null == myApplication) {
			myApplication = new MyApplication();
		}
		return myApplication;
	}

	public boolean isUpdateFriend() {
		return updateFriend;
	}

	public void setUpdateFriend(boolean updateFriend) {
		this.updateFriend = updateFriend;
	}

	public void resetUpdateInfo() {
		updateFriend = false;
	}

	public boolean isCheckUpdate() {
		return checkUpdate;
	}

	public void setCheckUpdate(boolean checkUpdate) {
		this.checkUpdate = checkUpdate;
	}

}
